import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from "@/views/Login";
import SystemAdmin from "@/views/SystemAdmin";
import DormitoryAdminManager from "@/views/DormitoryAdminManager";
import DormitoryAdminAdd from "@/views/DormitoryAdminAdd";
import DormitoryAdminUpdate from "@/views/DormitoryAdminUpdate";
import StudentAdd from "@/views/StudentAdd";
import StudentManager from "@/views/StudentManager";
import StudentUpdate from "@/views/StudentUpdate";
import BuildingAdd from "@/views/BuildingAdd";
import BuildingManager from "@/views/BuildingManager";
import BuildingUpdate from "@/views/BuildingUpdate";
import DormitoryAdd from "@/views/DormitoryAdd";
import DormitoryManager from "@/views/DormitoryManager";
import DormitoryUpdate from "@/views/DormitoryUpdate";
import MoveoutRegister from "@/views/MoveoutRegister";
import MoveoutRecord from "@/views/MoveoutRecord";
import AbsentRecord from "@/views/AbsentRecord";
import DormitoryAdmin from "@/views/DormitoryAdmin";
import AbsentRegister from "@/views/AbsentRegister";


Vue.use(VueRouter)

const routes = [
  {
    path: '/dormitoryAdmin',
    name: '宿舍管理员',
    component: DormitoryAdmin,
    redirect: '/absentRecord2',
    children: [
      {
        path: '/absentRecord2',
        name: '缺寝记录',
        component: AbsentRecord
      },
      {
        path: '/absentRegister',
        name: '缺寝登记',
        component: AbsentRegister
      }
    ]
  },
  {
    path: '/systemAdmin',
    name: '系统管理员',
    component: SystemAdmin,
    redirect: '/dormitoryAdminAdd',
    children: [
      {
        path: '/dormitoryAdminManager',
        name: '宿管管理',
        component: DormitoryAdminManager
      },
      {
        path: '/dormitoryAdminAdd',
        name: '宿管添加',
        component: DormitoryAdminAdd
      },
      {
        path: '/dormitoryAdminUpdate',
        name: '修改宿舍',
        component: DormitoryAdminUpdate
      },
      {
        path: '/studentAdd',
        name: '添加学生',
        component: StudentAdd
      },
      {
        path: '/studentManager',
        name: '学生管理',
        component: StudentManager
      },
      {
        path: '/studentUpdate',
        name: '修改学生',
        component: StudentUpdate
      },
      {
        path: '/buildingAdd',
        name: '添加楼宇',
        component: BuildingAdd
      },
      {
        path: '/buildingManager',
        name: '楼宇管理',
        component: BuildingManager
      },
      {
        path: '/buildingUpdate',
        name: '楼宇修改',
        component: BuildingUpdate
      },
      {
        path: '/dormitoryAdd',
        name: '添加宿舍',
        component: DormitoryAdd
      },
      {
        path: '/dormitoryManager',
        name: '宿舍管理',
        component: DormitoryManager
      },
      {
        path: '/dormitoryUpdate',
        name: '修改宿舍',
        component: DormitoryUpdate
      },
      {
        path: '/moveoutRegister',
        name: '迁出登记',
        component: MoveoutRegister
      },
      {
        path: '/moveoutRecord',
        name: '迁出记录',
        component: MoveoutRecord
      },
      {
        path: '/absentRecord',
        name: '缺寝记录',
        component: AbsentRecord
      }

    ]
  },
  {
    path: '/login',
    name: '登录',
    component: Login
  },
  {
    path: '/',
    name: '根目录',
    redirect: '/login',
  }
]


const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 前端路由拦截，必须登录才能访问其他页面
// 参数1 : to 目标路由对象
// 参数2 : from 来源路由对象
// 参数3 : next() 下一步
router.beforeEach((to, from, next) => {
  // 1. 判断是不是登录页面
  // 是登录页面
  if(to.path === '/login') {
    next()
  } else {
    // 不是登录页面
    // 2. 判断 是否登录过
    let systemToken = localStorage.getItem('systemAdmin')
    let dormitoryToken = localStorage.getItem('dormitoryAdmin')
    if(systemToken || dormitoryToken){
      next()
    }else{
      next('/login')
    }
  }
})

export default router
